import numpy as np
import csv
import random
from datetime import datetime
from time import strftime


def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = np.array([])
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey =np.append(sKey, temp)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def build_data_list_withoutColumnName(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.reader(file(fn), dialect="excel")
    i = 0
    for row in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        i += 1
        for item in row:
            temp = float(item)
            sKey.append(temp)
    #sKey.shape=(-1,i)
    sKey = np.array(sKey)
    sKey.shape = (i, -1)   
    return sKey
#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "========================================"
    print "begin at " + getCurTime()
    
    idCSV = 'C:/_DATA/CancerData/SatScan/neast_id.csv'
    dataMatrix = build_data_list_withoutColumnName(idCSV)
    id = np.array([])
    i = 0
    for item in dataMatrix[:,0]:
        id = np.append(id, [i, int(item)])
        i += 1
    id.shape = (-1, 2)
    id = id[np.argsort(id[:,1]),:]
    temp_id = []
    for item in id[:,0]:
        temp_id.append(int(item))

    caseCSV_file = 'C:/_DATA/CancerData/SatScan/mult6000/three'        
    for i in range(5):
        temp_i = 2**i
        if temp_i < 10:
            caseCSV = caseCSV_file + '0' + str(temp_i) + '.csv'
        else:
            caseCSV = caseCSV_file + str(temp_i) + '.csv'
        print caseCSV
        dataMatrix = build_data_list_withoutColumnName(caseCSV)
        iLen = dataMatrix.shape
        #print dataMatrix
        
        temp_data = dataMatrix.T
        temp_data = temp_data[temp_id, :]

        output = np.array(range(iLen[0]))    
        output = np.append(output, temp_data)
        output.shape = (-1, iLen[0])

        caseCSV = caseCSV[:-4] + '_format.csv'
        np.savetxt(caseCSV, output, delimiter=',', fmt='%i')
    print caseCSV
    print "end at " + getCurTime()


